package com.servlet;



import com.model.Personnel;
import com.service.PersonnelService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/PersonnelServlet")
public class PersonnelServlet extends HttpServlet {
        PersonnelService ps=new PersonnelService();
        //全局出来方便调用
        HttpServletRequest request;
        HttpServletResponse response;
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                //接受页面的判断me，判断当前的方法执行的什么内容
                this.request=request;
                this.response=response;
                String me =request.getParameter("method");
                if (me.equals("show")){
                        this.show();
                }
                if (me.equals("add")){
                        this.add();
                }
                if (me.equals("delete")){
                        this.delete();
                }
                if (me.equals("toupdate")){
                        this.toupdate();
                }
                if (me.equals("update")){
                        this.update();
                }

        }

        private void add() throws IOException {
                String pname = request.getParameter("pname");
                String age = request.getParameter("age");
                String sex = request.getParameter("sex");
                String pgroup = request.getParameter("pgroup");
                String pposition = request.getParameter("pposition");
                String entry_days = request.getParameter("entry_days");
                ps.add (pname,age,sex,pgroup,pposition,entry_days);
//                response.getWriter().print("<script>parent.showPersonnel()</script>");
        }

        private void update() throws IOException {
                String pid =request.getParameter("pid");
                String pname = request.getParameter("pname");
                String age = request.getParameter("age");
                String sex = request.getParameter("sex");
                String pgroup = request.getParameter("pgroup");
                String pposition = request.getParameter("pposition");
                String entry_days = request.getParameter("entry_days");
                ps.update (pid,pname,age,sex,pgroup,pposition,entry_days);
//                response.setContentType("text/html; charset=UTF-8");
//                response.getWriter().print("<script>parent.showPersonnel()</script>");
        }
        private void toupdate() throws ServletException, IOException {
                String id = request.getParameter("id");
                Personnel personnel=ps.toupdate(id);
                request.setAttribute("personnel",personnel);
                System.out.println(personnel);
                request.getRequestDispatcher("personnel/PersonnelUpdate.jsp").forward(request,response);

        }

        private void delete() throws ServletException, IOException {
                String id=request.getParameter("id");
                ps.delete(id);
                this.show();
        }


        private void show() throws ServletException, IOException {
                //从service中获取集合数据为DB
                List<Personnel> personnels=ps.show();
                //将数据传给页面
                request.setAttribute("personnels",personnels);
                //跳转到指定页面
                request.getRequestDispatcher("personnel/show.jsp").forward(request,response);
        }
}
